// 粘性组件
import 'package:flutter/material.dart';

class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  final PreferredSizeWidget tabbar;
  final Color? backgroundColor;
  final bool showTop;
  final double minTopHeight;
  final double maxTopHeight;
  final Widget top;

  StickyTabBarDelegate({
    required this.tabbar,
    this.backgroundColor,
    this.showTop = false,
    this.minTopHeight = 0,
    this.maxTopHeight = 0,
    this.top = const SizedBox(),
  });

  @override
  double get minExtent => tabbar.preferredSize.height + minTopHeight;

  @override
  double get maxExtent => tabbar.preferredSize.height + maxTopHeight;

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    // shrinkOffset：child偏移值 minExtent~maxExtent
    // overlapsContent：SliverPersistentHeader覆盖其他子组件返回true，否则返回false

    overlapsContent = shrinkOffset >= maxTopHeight - minTopHeight;

    return SizedBox.expand(
      child: Container(
        alignment: Alignment.bottomCenter,
        decoration: BoxDecoration(
          color: backgroundColor,
          boxShadow: overlapsContent
              ? [
                  BoxShadow(
                    color: Color.fromRGBO(136, 136, 136, 1),
                    blurRadius: 4,
                    spreadRadius: 1,
                  )
                ]
              : [],
        ),
        child: Column(
          children: [
            Expanded(
              child: overlapsContent ? SizedBox() : top,
            ),
            tabbar,
          ],
        ),
      ),
    );
  }

  @override
  bool shouldRebuild(StickyTabBarDelegate oldDelegate) {
    return true;
  }
}
